package com.generic.mybatis.dal.interceptor.automap.builder;

import com.generic.mybatis.dal.interceptor.automap.SqlBuilder;
import com.generic.mybatis.dal.interceptor.automap.TableMapper;
import com.generic.mybatis.dal.interceptor.automap.TableMapperHolder;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.MappedStatement;

import static com.generic.mybatis.dal.interceptor.automap.builder.SqlBuilderUtil.buildCritialStatement;

/**
 * select count(1) from table
 *
 * @author wuaj
 */
public class CountBuilder implements SqlBuilder {
    @Override
    public String buildSql(MappedStatement mappedStatement, Class dtoClass, Object parameterObject) {
        SQL sql = new SQL();
        TableMapper tableMapper = TableMapperHolder.getTableMapper(dtoClass);
        sql.SELECT("count(1)");
        tableMapper.buildFromStatement(sql);
        buildCritialStatement(tableMapper, sql, parameterObject, "array");
        return sql.toString();
    }
}
